// pages/index/index.js
//引入模块
const regeneratorRuntime = require("../../utils/regeneratorRuntime.js");
const { AddTrace } = require("../../utils/addCustTraceIns.js");
var app = getApp();
var mySelf = "";
let officialDomain = app.globalData.officialDomain;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [{
        carouseladdress: "https://zhuanyoyo.oss-us-west-1.aliyuncs.com/customerApplet/index/banner/banner11.jpg"
      }, {
        carouseladdress: "https://zhuanyoyo.oss-us-west-1.aliyuncs.com/customerApplet/index/banner/banner22.jpg"
      }, {
        carouseladdress: "https://zhuanyoyo.oss-us-west-1.aliyuncs.com/customerApplet/index/banner/banner33.jpg"
      }],
    qyContData: [], //地区
    msContData: [], //美食
    yhContData: [  //优惠
      { yhName: "全部优惠", couponType: "", isCheck: true },
      { yhName: "代金券", couponType: 0, isCheck: false },
      { yhName: "折扣券", couponType: 1, isCheck: false },
      { yhName: "赠品", couponType: 2, isCheck: false},
      ],
    // 默认下拉导航显示
    addr: "地区",
    ms: "美食",
    yh: "优惠",
    // 下拉导航隐藏显示
    qyopen: false,
    qyshow: true,
    msopen: false,
    msshow: true,
    yhopen: false,
    yhshow: true,
    isfull: false,
    shownavindex: '',
    initData: { shopArea: "", classId: "", couponType: "", pageNum:1,pageSize:20 },//初始化请求数据
    verSwiperList: [], //垂直滚动轮播
    storeLeng:[],
    showLoading: false, 
    allCount: 0,              //总的页数
    allData: false, //是否加载完所有的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    mySelf = this;
    //同步初始化界面
    this.doIt();   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initNotice();   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.initData.pageNum * this.data.initData.pageSize < this.data.allCount) {
      var theInitData = this.data.initData;
      ++theInitData.pageNum;
      this.setData({
        showLoading: true,
        initData: theInitData
      });
      this.initPageData();
    } else {
      this.setData({
        showLoading: false,
        allData: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 自定义事件

  //跳转页面
  // 通过data-params带上跳转路径
  toOpen(e){
    var item = e.currentTarget.dataset.params;
      wx.navigateTo({
        url: '../' + item + '/' + item,
      })     
  },

  onPageScroll: function (object) {
    if (object.scrollTop >= "190") {
      this.setData({
        st: true
      })
    } else {
      this.setData({
        st: false
      })
    }
  },

  //选择区域
  listqy: function (e) {
    // console.log(this.data.qyopen)
    if (this.data.qyopen) {
      this.setData({
        qyopen: false,
        msopen: false,
        yhopen: false,
        msshow: true,
        yhshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        qyopen: true,
        yhopen: false,
        msopen: false,
        msshow: true,
        yhshow: true,
        qyshow: false,
        isfull: true,
        shownavindex: 1
      })
    }

  },
  //美食选择
  listms: function (e) {
    // console.log(this.data.msopen)
    if (this.data.msopen) {
      this.setData({
        msopen: false,
        yhopen: false,
        qyopen: false,
        msshow: true,
        yhshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        msopen: true,
        yhopen: false,
        qyopen: false,
        msshow: false,
        yhshow: true,
        qyshow: true,
        isfull: true,
        shownavindex: 2
      })
    }
  },
  //选择优惠
  listyh: function (e) {
    // console.log(this.data.yhopen)
    if (this.data.yhopen) {
      this.setData({
        msopen: false,
        yhopen: false,
        qyopen: false,
        msshow: true,
        yhshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        msopen: false,
        yhopen: true,
        qyopen: false,
        msshow: true,
        yhshow: false,
        qyshow: true,
        isfull: true,
        shownavindex: 3
      })
    }
  },

  // 选择地址
  selectaddr: function (e) {
    this.data.qyContData.map((item,index)=>{
      if (e.currentTarget.dataset.addr.shopArea === item.shopArea){
        item.isCheck = true;
      }else{
        item.isCheck = false;
      }
      return item;
    })
    this.setData({
      addr: e.currentTarget.dataset.addr.areaName,
      qyContData: this.data.qyContData,
      storeLeng:[]
    })
    this.listqy();
    this.data.initData.pageNum = 1;
    this.data.initData.shopArea = e.currentTarget.dataset.addr.shopArea;
    wx.pageScrollTo({
      scrollTop:0
    })
    this.initPageData();
  },

  // 选择美食
  selectms: function (e) {
    this.data.msContData.map((item, index) => {
      if (e.currentTarget.dataset.ms.classId === item.classId) {
        item.isCheck = true;
      } else {
        item.isCheck = false;
      }
      return item;
    })
    this.setData({
      ms: e.currentTarget.dataset.ms.shopStyleName,
      msContData: this.data.msContData,
      storeLeng: []
    })
    this.listms();
    this.data.initData.pageNum = 1;
    this.data.initData.classId = e.currentTarget.dataset.ms.classId;
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.initPageData();
  },

  // 选择优惠
  selectyh: function (e) {
    this.data.yhContData.map((item, index) => {
      if (e.currentTarget.dataset.yh.couponType === item.couponType) {
        item.isCheck = true;
      } else {
        item.isCheck = false;
      }
      return item;
    })
    this.setData({
      yh: e.currentTarget.dataset.yh.yhName,
      yhContData: this.data.yhContData,
      storeLeng: []
    })
    this.listyh();
    this.data.initData.pageNum = 1;
    this.data.initData.couponType = e.currentTarget.dataset.yh.couponType;
    wx.pageScrollTo({
      scrollTop:0
    })
    this.initPageData();
  },
  //关闭遮罩层
  hidebg: function (e) {
    this.setData({
      qyopen: false,
      msopen: false,
      yhopen: false,
      msshow: true,
      yhshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0
    })
  },

  // 调用接口


  // 定义事件
  async doIt(){
    //地区初始化
    await mySelf.initShopArea();
     //美食初始化
    await this.initShopStyle();
    //初始化界面
    await this.initPageData();
  },

  //地区初始化
  initShopArea(){
    return new Promise((resolve,reject)=>{
    var unshiftAreaData = { areaName: "全部地区", shopArea: "", isCheck: true };
      wx.request({
        url: officialDomain + 'shopInfo/getShopArea',
        success: function (res) {
          // console.log("地区"+res);
          if (res.data.errorCode == "0000") {
            //取没每图第一张
            res.data.data.map((item, index) => {
              item.isCheck = false;  //餐厅特色
              item.shopArea = item.areaId;
              return item;
            })
            res.data.data.unshift(unshiftAreaData);
            mySelf.setData({
              qyContData: res.data.data,
            })
            resolve();
          }
        }
      })
    })
  },

  //美食初始化
  initShopStyle() {
    return new Promise((resolve, reject) => {
    var unshiftStyleData = { shopStyleName: "全部美食", classId: "", isCheck: true };
    wx.request({
      url: officialDomain + 'shopInfo/getShopStyle',
      success: function (res) {
        // console.log("美食" +res);
        if (res.data.errorCode == "0000") {
          //取没每图第一张
          res.data.data.map((item, index) => {
            item.isCheck = false;  //餐厅特色
            item.classId = item.shopStyleId;
            return item;
          })
          res.data.data.unshift(unshiftStyleData);
          // console.log(res.data.data)
          mySelf.setData({
            msContData: res.data.data,
          })
          resolve();
        }
      }
      })
    })
  },

  initPageData() {
    return new Promise((resolve, reject) => {
    //  获取店铺列表数据
    var data = {
      ...this.data.initData
    };
    wx.showLoading({
      title: 'loading',
      icon: "none"
    })
    //分页查询列表数据/shopInfo/getShopConditions
    wx.request({
      url: officialDomain + 'shopInfo/getShopConditions',
      data: data,
      success: function (res) {
        // console.log(res);
        if (res.data.errorCode == "0000") {
          //取没每图第一张
          resolve();

          res.data.data.content.map((item, index) => {
            // console.log(item)
            item.features = item.features ? item.features.split(";") : [];  //餐厅特色
            item.shopImage = item.shopImage ? JSON.parse(item.shopImage) : [];  //餐厅特色
            //设置所属区域
            mySelf.data.qyContData.map((qyItem,qyIndex)=>{
              // console.log(qyItem);
              if(item.shopArea == qyItem.shopArea){
                item.qyItemName = qyItem.areaName
              }
            })
            //设置所属美食
            mySelf.data.msContData.map((msItem, msIndex) => {
              // console.log(msItem);
              if (item.classId == msItem.classId) {
                item.msItemName = msItem.shopStyleName
              }
            })
            return item;
          })
          // console.log(res.data.data.content)
          mySelf.data.storeLeng = mySelf.data.storeLeng.concat(res.data.data.content);
          mySelf.setData({
            storeLeng: mySelf.data.storeLeng,
            allCount: res.data.data.totalElements,
            showLoading: false,
          })

          if (res.data.data.content.length){
            mySelf.setData({
              allData: true
            })
          }
          
        }
      },
      complete: function (res) {
        wx.hideLoading();
      }
      })
    })


  },

  // 获取公告
  initNotice(){
    wx.request({
      url: officialDomain + 'notice/getNotice',
      success: function (res) {
        // console.log(res);
        if (res.data.errorCode == "0000") {
          mySelf.setData({
            verSwiperList: res.data.data
          })
        }
      }
    })

  },

  // 通过data-params带上路径
  toOpen2(e) {
    var itemUrls = e.currentTarget.dataset.urls;
    var itemParams = e.currentTarget.dataset.params;
    wx.navigateTo({
      url: '../' + itemUrls + '/' + itemUrls + '?params=' + itemParams + '&isForward=2'
    })
  },

  //点击banner埋点
  addTraceBanner(e){
    var carouseaddr = e.currentTarget.dataset.carouseaddr;
    AddTrace(officialDomain, { userId: wx.getStorageSync("userId"), shopId: "", cPage: "首页", cOper: "banner：" +carouseaddr + " 被点击", cNote: "首页banner埋点" });
  },

  //点击公告埋点
  addTraceNotice(e) {
    var notice = e.currentTarget.dataset.notice;
    AddTrace(officialDomain, { userId: wx.getStorageSync("userId"), shopId: "", cPage: "首页", cOper: "公告：" + notice + " 被点击", cNote: "首页公告埋点" });
    this.toOpen2(e);
  },

  // v1.1.0
  // 截获竖向滑动
  catchTouchMove: function (res) {
    return false
  },

})